package com.li.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ndboo.reslibrary.R;

import java.util.List;

/**
 * Author: Li
 * Date: 2017/9/14 0014
 * Description:
 */

public class LayoutPop extends FrameLayout {
    private RotateAnimation mAnimationDismiss;
    private RotateAnimation mAnimationShow;
    private Context mContext;
    private View mView;

    public LayoutPop(@NonNull Context context) {
        super(context);
    }

    public LayoutPop(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init(context,attrs);
    }

    private TextView mTxtPop;
    private ImageView mImgPop;
    private PopupWindow mPopupWindow;
    private List<PopupWindow> mPopupWindows;
    private void init(Context context, AttributeSet attrs) {
        mView = LayoutInflater.from(context).inflate(R.layout.layout_pop,this);
        mTxtPop= mView.findViewById(R.id.txt_pop);
        mImgPop= mView.findViewById(R.id.img_pop);
        mAnimationShow = new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        mAnimationShow.setDuration(300);
        mAnimationShow.setFillAfter(true);
        mAnimationDismiss = new RotateAnimation(-180,0, Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);
        mAnimationDismiss.setDuration(300);
        mAnimationDismiss.setFillAfter(true);
    }
//    public void showPop(View view){
//        if (mPopupWindow == null) {
//            View content=LayoutInflater.from(mContext).inflate(R.layout.view_layout_pop,null);
//
//            RecyclerView recyclerView=content.findViewById(R.id.recycler_content);
//            mPopupWindow=new PopupWindow(content,-1,-1);
//            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//            PopAdapter popAdapter=new PopAdapter(mContext);
//            popAdapter.setItemClickListener(new PopAdapter.OnItemClickListener() {
//                @Override
//                public void onClick(View view, String text) {
//                    setPopText(text);
//                    mPopupWindow.dismiss();
//                }
//            });
//            recyclerView.setAdapter(popAdapter);
//            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//                @Override
//                public void onDismiss() {
//                    mImgPop.startAnimation(mAnimationDismiss);
//                    mView.setSelected(false);
//                }
//            });
//            mPopupWindows.add(mPopupWindow);
//            content.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mPopupWindow.dismiss();
//                }
//            });
//        }
//        if (mPopupWindow.isShowing()) {
//            mPopupWindow.dismiss();
//        }else {
//            mView.setSelected(true);
//            for (PopupWindow popupWindow : mPopupWindows) {
//                if (popupWindow.isShowing()) {
//                    popupWindow.dismiss();
//                }
//            }
//            int[] location=new int[2];
//            view.getLocationInWindow(location);
//            view.measure(0,0);
//            int height=view.getMeasuredHeight();
//            mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY,0,location[1]+height);
//            mImgPop.startAnimation(mAnimationShow);
//        }
//    }
//
//    public void setPopText(String txt){
//        mTxtPop.setText(txt);
//    }
}
